//This originally was copied from the "Updated_Results_SEP23" script

//Table A3 final run
log using "$rootpath/log/Table_A3_Fig5_Results.log", replace
eststo clear
use "$rootpath/processed_data/Estimation_Data.dta", clear
drop if key_estimation_sample==0
label variable lead_5 "-4"
label variable lead_4 "-3"
label variable lead_3 "-2"
label variable lead_2 "-1"
label variable lead_1 "0"
label variable lag_1 "1"
label variable lag_2 "2"
label variable lag_3 "3"
label variable lag_4 "4"
label variable lag_5 "5"
sort facility_id year
egen num_obs=nvals(year), by(facility_id)

rename lead_1 lead_0
//facility-level errors
reghdfe  log_damage55 lead_5 lead_4 lead_3 lead_2 lead_0 lag_* if num_obs==10&min_year==2008, absorb(facility_id naics#year) vce(cluster facility_id)
estimates store event_study44
coefplot event_study44, level(99) vert keep( lead_5 lead_4 lead_3 lead_2 lead_0 lag_1 lag_2 lag_3 lag_4 lag_5) yline(0) nokey omitted ytitle("Impact on Inverse Hyperbolic Sine (Damages)") xtitle("Years from Policy Change") ciopts(recast(rcap)) graphregion(color(white))
graph export "$rootpath/table_graph/Event_Study_Damages.pdf", replace

//state-level errors
reghdfe  log_damage55 lead_5 lead_4 lead_3 lead_2 lead_0 lag_* if num_obs==10&min_year==2008, absorb(facility_id naics#year) vce(cluster state_number)

//facility-level se
reghdfe  log_emissions4 lead_5 lead_4 lead_3 lead_2 lead_0 lag_* if num_obs==10&min_year==2008, absorb(facility_id naics#year) vce(cluster facility_id)
estimates store event_studyem4
coefplot event_studyem4, level(99) vert keep( lead_5 lead_4 lead_3 lead_2 lead_0 lag_1 lag_2 lag_3 lag_4 lag_5) yline(0) nokey omitted ytitle("Impact on Inverse Hyperbolic Sine (Emissions)") xtitle("Years from Policy Change") ciopts(recast(rcap)) graphregion(color(white))
graph export "$rootpath/table_graph/Event_Study_Emissions.pdf", replace

//state-level se
reghdfe  log_emissions4 lead_5 lead_4 lead_3 lead_2 lead_0 lag_* if num_obs==10&min_year==2008, absorb(facility_id naics#year) vce(cluster state_number)

esttab event_study44 event_studyem4 using "$rootpath/table_graph/EVENT_STUDY_Results_A3.tex",  star( * 0.1 ** 0.05 *** 0.01) cells(b(star fmt(3)) se(par fmt(3))) keep(lead_5 lead_4 lead_3 lead_2 lag_*) scalars(N F) ar2 replace 
log close
